@layout("/common/_container.html"){
<div class="layui-card">
	<div class="layui-form layui-card-header layuiadmin-card-header-auto">
	  <div class="layui-form-item">
		  <div class="layui-inline">
			 <label class="layui-form-label">新闻分类</label>
			  <div class="layui-input-block">
			  <select name="categoryId" id="categoryId" lay-verify="required">
				  <option value="">所有分类</option>
				  @for(category in categories){
				  <option value="${category.categoryId}">${category.categoryName}</option>
				  @}
			  </select>
			  </div>
	  	</div>
	    <div class="layui-inline">
	      	<label class="layui-form-label">文章标题</label>
			<div class="layui-input-block">
			  <input type="text" id="newsName" id="newsName" placeholder="文章标题" autocomplete="off" class="layui-input">
			</div>
	    </div>
	    <div class="layui-inline">
	      <button class="layui-btn layuiadmin-btn-useradmin" data-type="reload">
	        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
	      </button>
	    </div>
	  </div>
	</div>
  <div class="layui-card-body">
  @if(shiro.hasPermission("/news/add")){
  <div style="padding-bottom: 10px;">
      <button class="layui-btn layuiadmin-btn-useradmin" data-type="add"><i class="layui-icon layui-icon-add-1"></i>添加新闻</button>
  </div>
  @}
  <table class="layui-hidden" id="table" lay-filter="table"></table>
  	<script type="text/html" id="oper">
		  	@if(shiro.hasPermission("/news/update")){
  			<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" title="编辑"><i class="layui-icon layui-icon-edit"></i></a>
			@}    
			@if(shiro.hasPermission("/news/delete")){  
			<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" title="删除"><i class="layui-icon layui-icon-delete"></i></a>
			@}
			
	</script>
  </div>
</div>

<script>
layui.config({
	base: Feng.ctxPath + "/static/layuiadmin/" //静态资源所在路径
}).extend({
	index: 'lib/index' //主入口模块
}).use(['index', 'layer','table', 'form'],function(){
	
	var $ = layui.$
  	,form = layui.form
  	,table = layui.table;
	var loadingIndex = layer.msg('数据加载中', {icon: 16, time: 0 });
	table.render({
	    elem: '#table'
	    ,url: Feng.ctxPath + '/news/list'
	    ,cellMinWidth: 100
	    ,height: 'full-168' //设置高度
	    ,cols: [[
            {title: '序号', type:'numbers'},
	        {title: '分类名称', field: 'categoryName'},
	        {title: '文章标题', field: 'newsName'},
	        {title: '文章简介', field: 'newsDesc'},
	        {title: '排序值', field: 'newsNum'},
	        {title: '发布时间', field: 'createTime'},
	        {title: '操作',fixed: 'right', width:140, align:'center',fixed: 'right', toolbar: '#oper'}
	    ]]
	    ,page:true
	    ,loading:true
	    ,text: {
	        none: '暂无相关数据' 
	      }
	    ,done: function(res){
	    	layer.close(loadingIndex);
        }
	});
	
	//事件
	var active = {
		//加载
	   	reload: function(){
	       var newsName = $('#newsName').val();
	       var categoryId = $('#categoryId').val();
	       var loadingIndex = layer.msg('数据加载中', {icon: 16, time: 0 });
	       //执行表格重载
	       table.reload('table', {
	    	 height: 'full-168' //设置高度
	    	 ,where: {
                   newsName: newsName,
                   categoryId: categoryId
	         }
	       ,page:true
	       ,done: function(res){
		    	layer.close(loadingIndex);
	        }
	      });
	    },
     	add: function(){
     	 var loadingIndex = layer.msg('数据加载中', {icon: 16, time: 0 });
     	 location.href = Feng.ctxPath + '/news/news_add';
       }
   };

   $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
     var type = $(this).data('type');
     active[type] ? active[type].call(this) : '';
   });
	

 //监听工具条
   table.on('tool(table)', function(obj){
     var data = obj.data;
     if(obj.event === 'del'){
    	 layer.confirm('确定删除此条新闻吗？', function(index){
    		 var loadingIndex = layer.msg('数据加载中', {icon: 16, time: 0 ,shade: 0.01});
    		 var ajax = new $ax(Feng.ctxPath + "/news/delete", function (data) {
                 Feng.success("删除成功!");
                 table.reload('table',{
               	  done: function(res){
          		    	layer.close(loadingIndex);
          	          }
                  }); //数据刷新
                 layer.close(index); //关闭弹层
             }, function (data) {
                 Feng.error("删除失败!" + data.responseJSON.message + "!");
             },true);
             ajax.set("newsId", data.newsId);
             ajax.start();
         });
     }else if(obj.event === 'edit'){
       var loadingIndex = layer.msg('数据加载中', {icon: 16, time: 0 });
       location.href = Feng.ctxPath + '/news/news_update/'+data.newsId;
     }
   });
});



   
</script>
@}
